package com.egghiro.framework.j2se.game.map.maze.creator;

import java.util.ArrayList;
import java.util.List;

import com.egghiro.framework.j2se.game.map.maze.cons.MazeConst;
import com.egghiro.framework.j2se.game.model.element.Index;
import com.egghiro.framework.j2se.util.RandomComposite;

public class PoleDownMazeCreator extends MazeCreator {
    protected Index[][] poleIndexArray;
    protected ArrayList<Index> offsetIndexList1;
    protected ArrayList<Index> offsetIndexListOther;
    protected RandomComposite<Index> randomOffsetComposite;

    // private Random random;

    public PoleDownMazeCreator() {
        this.offsetIndexListOther = new ArrayList<Index>();
        this.offsetIndexListOther.add(new Index(1, 0));// 右
        this.offsetIndexListOther.add(new Index(-1, 0));// 左
        this.offsetIndexListOther.add(new Index(0, 1));// 下

        this.offsetIndexList1 = new ArrayList<Index>(offsetIndexListOther);
        this.offsetIndexList1.add(new Index(0, -1));// 上

        randomOffsetComposite = new RandomComposite<Index>();
    }

    @Override
    protected boolean checkSize(int xSize, int ySize) {
        return super.checkSize(xSize, ySize) && (xSize % 2 == 1) && (ySize % 2 == 1);
    }

    @Override
    protected String getMazeSizeExceptionMessage() {
        return "幅は5以上の奇数にしてください。";
    }

    @Override
    protected void createMaze() {
        // 外周を壁で埋める
        this.makeOuterWall();
        this.makePole();
        this.downPole();
    }

    protected void makePole() {
        poleIndexArray = new Index[mazeArray.length / 2 - 1][mazeArray[0].length / 2 - 1];
        for (int i = 2; i < mazeArray.length - 1; i += 2) {
            for (int j = 2; j < mazeArray[i].length - 1; j += 2) {
                poleIndexArray[i / 2 - 1][j / 2 - 1] = new Index(i, j);
                mazeArray[i][j] = MazeConst.WALL;
            }
        }
        // for(int i = 0; i<poleIndexArray.length;i++){
        // for(int j = 0; j<poleIndexArray[i].length;j++){
        // System.out.print(poleIndexArray[i][j].getX() + "," +
        // poleIndexArray[i][j].getY() + " ");
        // }
        // System.out.println();
        // }
    }

    protected void downPole() {

        for (int j = 0; j < poleIndexArray[0].length; j++) {
            downPole(poleIndexArray[0][j], this.offsetIndexList1);
        }

        for (int i = 1; i < poleIndexArray.length; i++) {
            for (int j = 0; j < poleIndexArray[i].length; j++) {
                downPole(poleIndexArray[i][j], this.offsetIndexListOther);
            }
        }
    }

    protected void downPole(Index poleIndex, List<Index> offsetIndexList) {
        randomOffsetComposite.addAll(offsetIndexList);
        while (randomOffsetComposite.isEmpty() == false) {
            Index offsetIndex = randomOffsetComposite.randomPop();
            if (mazeArray[poleIndex.getI() + offsetIndex.getI()][poleIndex.getJ() + offsetIndex.getJ()] == MazeConst.RODE) {
                mazeArray[poleIndex.getI() + offsetIndex.getI()][poleIndex.getJ() + offsetIndex.getJ()] = MazeConst.WALL;
                randomOffsetComposite.clear();
                return;
            }
        }
    }

}
